package fairCandySwap;

import java.util.Arrays;

public class Solution {
    public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
        //
//        int alceSum = Arrays.stream(aliceSizes).sum();
//        int bobSum = Arrays.stream(bobSizes).sum();
//        for (int i = 0; i < aliceSizes.length; i++) {
//            for (int j = 0; j < bobSizes.length; j++) {
//                if((alceSum - aliceSizes[i] + bobSizes[j]) == (bobSum - bobSizes[j] + aliceSizes[i])){
//                    return new int[]{aliceSizes[i],bobSizes[j]};
//                }
//            }
//        }
//        return null;

        Arrays.sort(aliceSizes);
        Arrays.sort(bobSizes);
        int aliceSum = Arrays.stream(aliceSizes).sum();
        int bobSum = Arrays.stream(bobSizes).sum();
        int need = aliceSum - bobSum;
        int i = 0;
        int j = 0;
        while(i<aliceSizes.length&&j<bobSizes.length){
            if(aliceSizes[i] - bobSizes[j] > need){
                j++;
            }else if(aliceSizes[i] - bobSizes[j] <need){
                i++;
            }else {
                return new int[]{aliceSizes[i],bobSizes[j]};
            }
        }
        return null;
    }
}
